package com.woniuxy.course.service.impl;


import com.woniuxy.commons.dto.CourseCategoryDto;
import com.woniuxy.commons.entity.CourseCategory;
import com.woniuxy.course.mapper.CourseCategoryMapper;
import com.woniuxy.course.service.CourseCategoryService;
import com.woniuxy.course.utils.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author LuoChen
 * @DateTime 2020-07-16 17:40:20
 * @Description 课程分类的业务实现类
 */
@Service
public class CourseCategoryServiceImpl implements CourseCategoryService {

    @Autowired
    private CourseCategoryMapper courseCategoryMapper;

    /**
     * 从DAO层获取所有课程类别的数据
     *
     * @return: java.util.List<com.woniuxy.commons.entity.CourseCategory>
     * @author: LuoChen
     * @dateTime: 2020-07-16 17:38:09
     */
    @Override
    public List<CourseCategory> findAll() {
        return courseCategoryMapper.findAll();
    }

    /**
     * 新增一个课程类别
     *
     * @param categoryName
     * @return: int
     * @author: LuoChen
     * @dateTime: 2020-07-16 19:10:27
     */
    @Override
    public int addCategory(String categoryName) {
        return courseCategoryMapper.addCategory(categoryName) > 0 ? StatusCode.OPERATION_SUCCESS : StatusCode.OPERATION_FAIL;
    }

    /**
     * 根据categoryId删除中的该课程类别记录
     *
     * @param categoryId
     * @return: int
     * @author: LuoChen
     * @dateTime: 2020-07-16 20:50:38
     */
    @Override
    public int deleteCategory(int categoryId) {
        return courseCategoryMapper.deleteCategory(categoryId) > 0 ? StatusCode.OPERATION_SUCCESS : StatusCode.OPERATION_FAIL;
    }

    /**
     * 根据查询到的分类类别封装成树形结构的实体
     *
     * @return: java.util.List<com.woniuxy.commons.dto.CourseCategoryDto>
     * @author: LuoChen
     * @dateTime: 2020-07-24 15:12:54
     */
    @Override
    public Map<Integer, CourseCategoryDto> getCategoryBeTree() {
        List<CourseCategory> categoryList = findAll();
        Map<Integer, CourseCategoryDto> dtoHashMap = new HashMap<>();
        int size = categoryList.size();
        //将list中的分类列表封装成树形结构的DTO
        for (int i = 0; i < size; i++) {
            CourseCategory courseCategory = categoryList.get(i);
            int parentId = courseCategory.getParentId();
            if (parentId == 0) {
                //如果是根节点分类，就new一个dto对象，再将这个分类信息放进去
                dtoHashMap.put(courseCategory.getCategoryId(), new CourseCategoryDto().setCurrentNode(courseCategory));
                continue;
            }
            //找到父节点,然后进入父节点的list
            //如果父节点就是根节点
            if (dtoHashMap.containsKey(parentId)) {
                CourseCategoryDto courseCategoryDto = dtoHashMap.get(parentId);
                addNewSubNode(courseCategory, courseCategoryDto);
            } else {
                //循环递归
                Set<Map.Entry<Integer, CourseCategoryDto>> entries = dtoHashMap.entrySet();
                //遍历父节点
                for (Map.Entry<Integer, CourseCategoryDto> dtoEntry :
                        entries) {
                    CourseCategoryDto dto = dtoEntry.getValue();
                    if (findParentNode(courseCategory, dto)) {
                        break;
                    }
                }
            }
        }
        return dtoHashMap;
    }

    public boolean findParentNode(CourseCategory courseCategory, CourseCategoryDto categoryDto) {
        int parentId = courseCategory.getParentId();
        List<CourseCategoryDto> subNodes = categoryDto.getSubNodes();
        if (subNodes != null) {
            for (CourseCategoryDto courseCategoryDto : subNodes) {
                if (courseCategoryDto.getCurrentNode().getCategoryId() == parentId) {
                    addNewSubNode(courseCategory, courseCategoryDto);
                    return true;
                }
                if (findParentNode(courseCategory, courseCategoryDto)) {
                    return true;
                }
            }

        } else if (categoryDto.getCurrentNode().getCategoryId() == parentId) {
            addNewSubNode(courseCategory, categoryDto);
            return true;
        }
        return false;
    }

    public boolean addNewSubNode(CourseCategory courseCategory, CourseCategoryDto newNode) {
        //给一个装子节点的集合
        if (newNode.getSubNodes() != null) {
            return newNode.getSubNodes().add(new CourseCategoryDto().setCurrentNode(courseCategory));

        }
        List<CourseCategoryDto> subList = new ArrayList<>();
        newNode.setSubNodes(subList);
        return newNode.getSubNodes().add(new CourseCategoryDto().setCurrentNode(courseCategory));
        

    }
}
